package com.zhang.common.controller;

import com.baomidou.kaptcha.Kaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 简单快速集成 Google Kaptcha验证码
 * KaptchaController
 *
 * @author zhangxin
 * @date 2021/7/8
 */
@Api(value = "Kaptcha验证码接口")
@RestController
@RequestMapping("/kaptcha")
public class KaptchaController {

    @Autowired
    private Kaptcha kaptcha;

    @ApiOperation(value = "render", notes = "生成验证码")
    @GetMapping("/render")
    public void render() {
        kaptcha.render();
    }

    @ApiOperation(value = "validDefaultTime", notes = "验证验证码，超时时间900s")
    @PostMapping("/valid")
    public void validDefaultTime(@RequestParam String code) {
        //default timeout 900 seconds
        kaptcha.validate(code);
    }

    @ApiOperation(value = "validCustomTime", notes = "验证验证码，超时时间60s")
    @PostMapping("/validTime")
    public void validCustomTime(@RequestParam String code) {
        kaptcha.validate(code, 60);
    }

}
